package com.ruoyi.project.pay;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.channel.channelOrder.domain.ChannelOrder;
import com.ruoyi.project.channel.channelOrder.service.IChannelOrderService;
import com.ruoyi.project.channel.uxInfo.domain.UxInfo;
import com.ruoyi.project.channel.uxInfo.service.IUxInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.*;

public class UXPayTimer {
    private static final Logger log = LoggerFactory.getLogger(UXPayTimer.class);
    private IChannelOrderService channelOrderService;
    private IUxInfoService uxInfoService;
    private PayServiceImpl payService;
    public UXPayTimer(){
        channelOrderService = SpringContextUtil.getBean(IChannelOrderService.class);
        uxInfoService = SpringContextUtil.getBean(IUxInfoService.class);
        payService = SpringContextUtil.getBean(PayServiceImpl.class);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    ChannelOrder searchC = new ChannelOrder();
                    searchC.setPayType("30");
                    searchC.setOrderStatus("0");
                    List<ChannelOrder> channelOrders = channelOrderService.selectChannelOrderList(searchC);
                    for(int i=0;i<channelOrders.size();i++){
                        getRed(channelOrders.get(i));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(),e);
                }
            }
        },0,30 * 1000);
    }

    private void getRed(ChannelOrder tmpOrder){
        new Thread(){
            @Override
            public void run() {
                // 获取交易id
                UxInfo search = new UxInfo();
                search.setAccount(tmpOrder.getAccount());
                List<UxInfo>  uxInfos = uxInfoService.selectUxInfoList(search);
                if(uxInfos.size()==0){
                    log.info("查询是否收款出错，状态未登录："+tmpOrder.getAccount());
                    return;
                }
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put("oemid","oem001");
                hashMap.put("Authorization","Bearer "+uxInfos.get(0).getAuthToken());
                String body = HttpUtils.sendGet("https://cloud.huaat.com/trade/order?id="+tmpOrder.getDdId(),hashMap,"");
                JSONObject jsonObject = JSONObject.parseObject(body);
                if(jsonObject.getBooleanValue("success") == false){
                    log.info("查询是否收款出错，："+tmpOrder.getAccount()+";"+body);
                    return;
                }
                JSONObject resultData = jsonObject.getJSONObject("data");
                if(resultData.getString("status").equals("S")){
                    String amount = resultData.getString("amount");
                    tmpOrder.setPayAmount(BigDecimal.valueOf(Double.valueOf(amount)));
                    tmpOrder.setOrderId(tmpOrder.getPtOrderno());
                    payService.updateOrder(tmpOrder);
                }
            }
        }.start();
    }
}
